unit ELAboutBox;

(*
** $Id: ELAboutBox.pas,v 1.2 2005/05/06 13:18:08 luca Exp $
*)

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, JvVersionInfo, ComCtrls, ExtCtrls;

type
  TNeedVersionEvent = procedure (Sender :TObject; var ObjectName :String; var ObjectVersion :String) of object;

  TELAboutBox = class(TComponent)
  private
    FTitle: String;
    FIcon: TIcon;
    FModuleList: TStrings;
    FOnNeedVersion: TNeedVersionEvent;
    procedure SetIcon(const Value: TIcon);
    procedure SetModuleList(const Value: TStrings);
    procedure GetVersion;
  public
    FVersionList :TStrings;
    ModuleName :TStrings;
    function Execute: Boolean;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Title :String read FTitle write FTitle;
    property Icon :TIcon read FIcon write SetIcon;
    property ModuleList :TStrings read FModuleList write SetModuleList;
    property OnNeedVersion :TNeedVersionEvent read FOnNeedVersion write FOnNeedVersion;
  end;

  TELFrmAboutBox = class(TForm)
    btnOK: TButton;
    edtVersion: TEdit;
    lvVersions: TListView;
    edtName: TEdit;
    edtCopyright: TEdit;
    imgLogo: TImage;
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    VersionInfo :TJvVersionInfo;
    VersionList :TStrings;
    procedure AddRow(PackageName, Version :String);
    procedure ReadVersions;
  public
    { Public declarations }
    PackageList :TStrings;
  end;

//procedure Register;

implementation

{$R *.RES}

{$R *.DFM}

var
  ELFrmAboutBox: TELFrmAboutBox;

{ TELFrmAboutBox }

procedure TELFrmAboutBox.AddRow(PackageName, Version: String);
var
  Item :TListItem;
begin
  Item := lvVersions.Items.Add;
  Item.Caption := PackageName;
  Item.SubItems.Add(Version);
end;

procedure TELFrmAboutBox.btnOKClick(Sender: TObject);
begin
  Close;
end;

procedure TELFrmAboutBox.FormCreate(Sender: TObject);
begin
  VersionInfo := TJvVersionInfo.Create(Application.ExeName);
end;

procedure TELFrmAboutBox.FormDestroy(Sender: TObject);
begin
  VersionInfo.Free;
end;

procedure TELFrmAboutBox.ReadVersions;
var
  i :Integer;
begin
  Height := lvVersions.Top + 60;
  if PackageList.Count > 0 then
  begin
    Height := Height + 30 + (18 * PackageList.Count);
    lvVersions.Height := 25 + 18 * PackageList.Count;
    lvVersions.Visible := True;
  end;

  Cursor := crHourGlass;
  try
    Caption := 'Informazioni su ' + VersionInfo.InternalName;
    edtName.Text := VersionInfo.ProductName;
    edtCopyright.Text := VersionInfo.LegalCopyright;
    edtVersion.Text := 'Versione ' + VersionInfo.FileVersion;
    Refresh;

    for i := 0 to PackageList.Count - 1 do
    begin
      AddRow(PackageList[i], VersionList[i]);
    end;

  finally
    Cursor := crDefault;
  end;
end;

procedure TELFrmAboutBox.FormActivate(Sender: TObject);
begin
  ReadVersions;
end;

{ TELAboutBox }

constructor TELAboutBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FIcon := TIcon.Create;
  FModuleList := TStringList.Create;
  FVersionList := TStringList.Create;
  ModuleName := TStringList.Create;
end;

destructor TELAboutBox.Destroy;
begin
  FIcon.Free;
  FModuleList.Free;
  FVersionList.Free;
  ModuleName.Free;
  inherited Destroy;
end;

function TELAboutBox.Execute: Boolean;
begin
  GetVersion;
  ELFrmAboutBox := TELFrmAboutBox.Create(Application);
  try
    if FTitle <> '' then
      ELFrmAboutBox.Caption := FTitle;
    if not FIcon.Empty then
      ELFrmAboutBox.imgLogo.Picture.Icon.Assign(FIcon)
    else
      ELFrmAboutBox.imgLogo.Picture.Icon.Handle := LoadIcon(MainInstance, 'MAINICON');
    ELFrmAboutBox.PackageList := ModuleName;
    ELFrmAboutBox.VersionList := FVersionList;
    result := ELFrmAboutBox.ShowModal = mrOK;
  finally
    ELFrmAboutBox.Free;
  end;
end;

procedure TELAboutBox.GetVersion;
var
  i :Integer;
  ObjVersion :String;
  ObjName :String;
begin
  FVersionList.Clear;
  ModuleName.Clear;
  for i := 0 to FModuleList.Count - 1 do
  begin
    ObjVersion := '1.0';
    ObjName := FModuleList[i];
    if Assigned(OnNeedVersion) then
      OnNeedVersion(self, ObjName, ObjVersion);
    FVersionList.Add(ObjVersion);
    ModuleName.Add(ObjName);
  end;
end;

procedure TELAboutBox.SetIcon(const Value: TIcon);
begin
  FIcon.Assign(Value);
end;

procedure TELAboutBox.SetModuleList(const Value: TStrings);
begin
  FModuleList.Assign(Value);
end;

{procedure Register;
begin
  RegisterComponents('EasyLib', [TELAboutBox]);
end;}

end.
